package cn.wokoba.service.querys.repository;


import cn.wokoba.dao.elasticsearch.IElasticSearchUserRaffleOrderMapper;
import cn.wokoba.dao.elasticsearch.po.UserRaffleOrder;
import cn.wokoba.service.querys.model.ESUserRaffleOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

//对运营提供的es查询仓储服务
@Slf4j
@Repository
public class ESUserRaffleOrderRepository {

    @Autowired
    private IElasticSearchUserRaffleOrderMapper elasticSearchUserRaffleOrderMapper;

    public List<ESUserRaffleOrderVO> queryESUserRaffleOrderVOList() {
        // 查询数据
        List<UserRaffleOrder> userRaffleOrders = elasticSearchUserRaffleOrderMapper
                .findAll(PageRequest.of(0, 10, Sort.by(Sort.Order.desc("updateTime"))))
                .getContent();
        // 封装数据
        List<ESUserRaffleOrderVO> esUserRaffleOrderVOS = new ArrayList<>();
        for (UserRaffleOrder userRaffleOrder : userRaffleOrders) {
            ESUserRaffleOrderVO esUserRaffleOrderVO = ESUserRaffleOrderVO.builder()
                    .userId(userRaffleOrder.getUserId())
                    .activityId(userRaffleOrder.getActivityId())
                    .activityName(userRaffleOrder.getActivityName())
                    .strategyId(userRaffleOrder.getStrategyId())
                    .orderId(userRaffleOrder.getOrderId())
                    .orderTime(userRaffleOrder.getOrderTime())
                    .orderState(userRaffleOrder.getOrderState())
                    .createTime(userRaffleOrder.getCreateTime())
                    .updateTime(userRaffleOrder.getUpdateTime())
                    .build();
            esUserRaffleOrderVOS.add(esUserRaffleOrderVO);
        }
        return esUserRaffleOrderVOS;
    }

}
